Docker containers use an internal file system that’s isolated from the host and other containers. This kata will teach us what happens when we make changes to a container file system. We’ll also learn how to create volumes that can be shared between containers and their hosts and between other containers.

Step 1: Make container file system changes#

The command to stop and remove all the containers is given below.

Note: The container IDs we see will be different.

The output will be something like this:

Stopping and removing all containers

Commands

Command/Parameter

Description

docker container stop $(docker container ls -q)

This stops all the containers.

docker container rm $(docker container ls -aq)

This removes all the containers.

The command to run the ubuntu container and execute the bash shell is given below.

After running the command above, the output will be something like this:

Running the Ubuntu container and executing the Bash shell

Commands

Parameter

Description

docker container

This is the parent command.

-it

This runs the container in interactive mode.

ubuntu

This is the name of the image to run.

bash

This is the command to run on the container. This is the bash shell.

This command runs an Ubuntu container and executes the Bash shell.

The command to create and list the file in the root of the container is given below.

The output will be something like this:

Creating and listing files in the root of the container

Commands

Command/Parameter

Description

ls

This lists files in the container.

echo "file1text" > file1.txt

This writes text into a new file called file1.txt.

ls

This lists files in the container. Note that the file1.txt file is now in the root directory of the container.

exit

This exits the bash shell, also causing the container to exit.

This sequence of commands creates a new file in the root of the container called file1.txt. The ls command displays the file. The exit command terminates the bash program, which exits the container.

The command to run the second Ubuntu container is given below.

After running the command above, the output displayed will be something like this:

Running the second Ubuntu container

Commands

Parameter

Description

docker container

This is the parent command.

-it

This runs the container in interactive mode.

ubuntu

This is the name of the image to run.

bash

This is the command to run on the container. This is the bash shell.

This command runs a second Ubuntu container and lists the files in the root. The ls command shows that the file1.txt file is not present in the second container. This is because the second container was a new instance of the ubuntu image. The Ubuntu image does not have the file1.txt file. New containers that are run from that image won’t have any changes made to other containers run from that image. The next steps in this kata will demonstrate how to share files between containers.

Step 2: Run a container with a named volume#

The command to mount a named volume to the container is given below.

The output will be something like this:

Mounting a named volume to the container

Commands

Parameter

Description

docker container

This is the parent command.

run

This runs a container.

-d

This runs a container in disconnected mode.

--name

This assigns a name to a container

web

This is the name to assign to the container.

-v

This mounts a volume to a container

myVolume:/webapp

This creates a new volume named myVolume and mounts it to a directory in the container called webapp.

nginx

This is the name of the image to run.

The Linux file system uses volumes. A volume is a logical file space, like a drive partition. Connecting a volume to the OS is referred to as mounting the volume.

This step demonstrates how to mount a named volume to a container. This step runs a new container, creates a new volume, and mounts the new volume to the container.

The -v paramter mounts a volume. This is followed by a value in the format [volume]:[path].

The name of the volume is myVolume, which is created if it doesn’t exist. The path in the container to which the volume is mounted is /webapp.

The command to list the running containers is given below.

We’ll see something like this when we run the command above:

Listing running containers

Commands

Parameter

Description

docker container

This is the parent command.

ls

This lists the running containers.

This command lists the running containers.

The command to list all the files in the container is given below.

The output will be something like this:

Listing all files in the container

Commands

Parameter

Description

docker container

This is the parent command.

exec

This executes a command on a running container.

web

This is the name of the container on which to run the command.

ls

This is the command to run. Note that there's a directory called webapp in the container.

This command lists all the files in the root of the web container. The /webapp folder is the mounted myVolume volume.

The command to list all the volumes is given below.

When we execute the command above, the output will be something like this:

Listing all the volumes

Commands

Parameter

Description

volume

This is the Docker volume object type.

ls

This lists all the volumes when run on the volume object type.

This command lists all of the volumes, including the myVolume volume created in the first command.

We learned in the first step that changes made to a container file system aren’t persisted to the image. Volumes are used to create persistent stores that can be shared between containers or between the container and its host. This is useful in cases where multiple containers need to share the same data, such as a configuration database or application code.

Step 3: Share a volume between containers#

The command to run and mount the NGNIX container is given below.

The output will be something like this:

Running and mounting the NGINX container

Commands

Parameter

Description

docker container

This is the parent command.

run

This runs a command in a running container.

-d

This runs a container in disconnected mode.

--name

This assigns a name to the container.

web2

This is the name to assign to the container.

-v

This mounts a volume to the container.

myVolume:/webapp

The name of the volume to mount is myVolume. After the colon is the path to which the volume is to be mounted, /webapp.

nginx

This is the container image to run.

Docker volumes can be shared between multiple containers. This step runs a second NGINX container called web2 and mounts the myVolume volume to the container. There are now two NGINX containers running, both with the myVolume volume mounted to the path /webapp.

The command to create a file in the /webapp volume and mount the path is given below.

The output that will be displayed after running this command will be something like this:

Creating a file in the /webapp volume and mounting a path called file1.txt

Parameter

Description

docker container

This is the parent command.

exec

This runs a command in a running container.

web

This is the name of the container on which to run the command.

/bin/sh

This is the command to run in the container. This is the Bourne shell.

-c

The -c parameter of the Bourne shell runs an inline script by default, whereas the shell typically runs a script stored in a file.

“echo ‘some text’ > webapp/file1.txt”

This is the inline script. This script writes text to a file inside the web container.

  • echo: This sends the following text to STDOUT.
  • some text: This is sent to STDOUT.
  • >: The greater-than operator redirects the STDOUT output to a file.
  • webapp/file1.txt: This is the name of the file to write.

This step runs a command inside the first container, web. The command creates a file in the /webapp volume mount path called file1.txt.

The command to list the files is given below.

The output will be something like this:

Listing files

Commands

Parameter

Description

docker container

This is the parent command.

exec

This runs a command in a running container.

[web] / [web2]

This is the name of the container on which to run the command.

ls webapp

This is the command to run. This lists the files in the webapp directory in the containers.

The /webapp directory of both containers is the mounted myVolume volume. The file was created in the web container, yet now shows in the /webapp directory of both containers. This is because myVolume is shared between the containers. The file listed in the first ls command is the same as the file listed in the second.

Step 4: Run a container with a host-mounted volume#

The command to create a new dockervolume directory is given below.

The result of command above will be something like this:

Creating a new directory

Commands

Command/Parameter

Description

mkdir dockervolume

This creates a directory called dockervolume.

cd dockervolume

This is used to change to the dockervolume directory.

echo file1text > file1.txt

echo file2text > file2.txt

This creates two files in the dockervolume directory.

ls

This lists the files in the dockervolume directory.

cd ..

This changes back to the /home/devops directory.

This sequence of commands creates a new dockervolume directory in the LVM, then creates two files (file1.txt and file2.txt) in that directory.

The command to run a new container with a host-mounted volume is given below.

When we execute the command above, the output will be something like this:

Running a new container with a host-mounted volume

Commands

Parameter

Description

docker container

This is the parent command.

run

This runs a container.

-d

This runs a container in disconnected mode.

--name

This assigns a name to a container.

web3

This is the name to assign to the container.

-v

This mounts a volume to the container.




$PWD/dockervolume:/hostmounted

This parameter mounts a directory on the host to a new volume in the container.

  • $PWD: This variable is the current directory (we can see this by executing echo $PWD).
  • $PWD/dockervolume: This is the full path to the dockervolume directory created in the first set of commands.
  • :/hostmounted: This is the volume to create and mount to the container. It's mounted at the root of the container filesystem.

nginx

This is the name of the image to run.

This command runs a new container with a host-mounted volume. Docker mounts to the local host path $PWD/dockervolume. This is also called a bind mount.

The syntax for this command is similar to the previous steps. This command specifies a local path instead of a volume name. This mounts a local directory on the host (the LVM) to a directory in the container (/hostmounted).

The command to list the contents of the directory is given below.

The output will be something like this:

Listing the contents of the directory

Commands

Parameter

Description

docker container

This is the parent command.

exec

This executes a command in a running container.

web3

This is the name of the container on which to run the command.

ls /hostmounted

This is the command to run within the container. This lists the contents of the /hostmounted volume.

This command lists the contents of the /hostmounted directory in web3.

Note that the two files, file1.txt and file2.txt, are listed in the /hostmounted directory in the container. They are the same files as the ones in the dockervolume directory in the LVM. Any changes made to those files will be reflected in the LVM and in the container.

Note: Bind mounts can be useful in some cases, but there are trade-offs. A container that uses a bind mount depends on files and directories on the host, which can prevent container portability. Docker recommends using shared volumes instead of bind mounts to avoid dependencies between containers and hosts.

Practice commands#

We’ve given a terminal and table containing a list of commands discussed in this lesson. Try out these commands after running the terminal, and check out the results!

Commands

Step

Command

This stops and removes all the containers and images.

docker container stop $(docker container ls -q)

docker container rm $(docker container ls -aq)

This runs a container from the ubuntu image interactively using bash.

docker container run -it ubuntu bash

This lists the files in the ubuntu container.

ls

This writes the text “file1text” to a file called file1.txt in the root folder.

echo "file1text" > file1.txt

This lists the files in the root folder again, allowing us to locate the file named file1.txt.

ls

This exits the container.

exit

This runs a second container interactively from the ubuntu image using bash.

docker container run -it ubuntu bash

This lists the files in the root (and check to see if file1.txt is present).

ls

This exits the container.

exit

This lists all the containers (running and stopped).

docker container ls -a

This runs a container named web from the nginx image with a named volume mapped to /webapp.


docker container run -d --name web -v=myVolume:/webapp nginx

This lists all the running containers.

docker container ls

This executes the ls command on the nginx container.

docker container exec web ls

This lists all the volumes.

docker volume ls

This runs a container named web2 from the nginx image with the same named volume mapped to /webapp.


docker container run -d --name web2 -v myVolume:/webapp nginx

This executes a command on the web container to write a file to the /webapp mounted volume.

docker container exec web /bin/sh -c "echo ‘some text’ > webapp/file1.txt"

This executes a command to list the contents of /webapp on the web container.

docker container exec web ls webapp

This executes a command to list the contents of /webapp on the web2 container.


docker container exec web2 ls webapp

This makes a directory called dockervolume and changes to it.

mkdir dockervolume

This write two files, file1.txt and file2.txt, to dockervolume.

cd dockervolume

This lists the contents of dockervolume.

echo file1text > file1.txt

This changes back to the parent directory.

echo file2text > file2.txt

ls

cd ..

This runs an NGINX container named web3 with a volume mounted to dockervolume.

docker container run -d --name web3 -v $PWD/dockervolume:/hostmounted nginx

This executes a command on web3 to list the contents of the dockervolume host-mounted volume.


docker container exec web3 ls /hostmounted

Terminal 1
Terminal

Click to Connect...

Docker Kata 2: Disconnected Containers

Docker Kata 4: Running a Web Server in a Container